﻿using AutoMapper;

using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.API;
using Warehouse.API.Applcation.Command.InventoryCommand;
using Warehouse.Domain.DTO;



namespace Warehouse.API.Controllers
{
    /// <summary>
    /// 仓库盘点控制器.写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class StocktakingController : ControllerBase
    {
        private readonly IMediator _mediator;
        public StocktakingController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加仓库盘点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddInventory(ADDInventoryDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                command.AddInventGuan.FirstOrDefault().user_id= Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.AddInventGuan.FirstOrDefault().office_id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.AddName = name;
                command.AddInventGuan.FirstOrDefault().AddName = name;
                command.InventoryType = "产品盘点";
                command.InventoryState = 1;//待审核
                result.Data = await _mediator.Send(new ADDInventoryCommand { dto=command});
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改仓库盘点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> UpdInventory(UpdInventoryDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                command.updInventGuan.FirstOrDefault().user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.updInventGuan.FirstOrDefault().office_id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.UpdName = name;
                command.updInventGuan.FirstOrDefault().UpdName = name;
                result.Data = await _mediator.Send(new UpdInventoryCommand { updDto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除仓库盘点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DelInventory(DelInventoryCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.DelName = name;
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改上传仓库盘点
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UplInventory(UpdInventoryDTO command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                command.updInventGuan.FirstOrDefault().user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.updInventGuan.FirstOrDefault().office_id = sid;
                //获取用户名
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;
                command.UpdName = name;
                command.updInventGuan.FirstOrDefault().UpdName = name;
                result.Data = await _mediator.Send(new UploadinventoryCommand { updDto = command });
            }
            catch (Exception ee)
            {
                result.Code = -1;
                result.Message = ee.Message;
                throw;
            }
            return result;
        }















    }
}
